package com.ztools.wx.provider;

import com.ztools.api.service.wx.WxMsgService;
import com.ztools.common.util.common.Result;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

/**
 * @Author:seeTools
 * @Date: 2023/4/9  0:14
 * @Version 1.0
 */
@Service(version = "1.0", timeout = 3000)
public class WxMsgServiceImpl implements WxMsgService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private WxMpService wxMpService;

    @Override
    public Result sendKeFuMessage(String openId, String content) {
        WxMpKefuMessage wxMpKeFuMessage = WxMpKefuMessage.TEXT().toUser(openId).content(content).build();
        try {
            boolean sendKeFuMessage = wxMpService.getKefuService().sendKefuMessage(wxMpKeFuMessage);
            if (sendKeFuMessage) {
                logger.info("客服消息发送成功:{}, {}", openId, content);
                return Result.success("客服消息发送成功");
            }
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        logger.warn("客服消息发送失败:{}, {}", openId, content);
        return Result.error("客服消息发送失败");
    }

}
